Fix media cache deadlocks when switching scenes - #1785
Conversation
Old memory manager has been rewritten from the ground. Replace per-update threads with a single worker that coalesces requests and manages cache budgets. Run media queries and cache-setting updates on the OBS graphics thread, and release source references outside locks. Cancel pending work safely during removal and shutdown. Add native tests for budgeting, readiness retries, source lifetime, and video resets.
Update the runtime preference when saving Advanced Settings and loading configuration at startup. Share the update path with SetMediaFileCaching. Add regression tests for preference changes and startup persistence.
| // Process only the batch taken above. After a SetCaching job, any follow-up | ||
| // QuerySource job must wait for a later tick so ffmpeg_source can process | ||
| // its deferred settings update first. | ||
| for (auto &result : results) { |
There was a problem hiding this comment.
[Claude-opus comment] potential Bug
memory-manager.cpp:252-259, triggered from osn-source.cpp:357-358
osn::Source::Update calls obs_source_update(src, sets) and then requestCacheUpdate(src). For ffmpeg_source the settings are written straight away, but the plugin's update() (which swaps the media player) is deferred until the source's next video_tick. In libobs, tick callbacks run before the sources are ticked (obs-video.c:48-56, then :78-84).
So when the user changes a looping local source from file A to file B, the first graphics tick after the request runs QuerySource like this:
- readSettings sees local_file = B (the new settings).
- get_file_info / get_pla A, because the deferredupdate hasn't run yet.
complete() then reserves A's size and queues an enable job for file == B. That
job passes its check, becay B. After that the wrongreservation never gets corrected:
- Once caching is on, quens early and neverre-measures.
- entry.file already equalse.
If B is much bigger than d for as long as the source lives. It can also go the other way and block other sources.
The comment at :249-251 already handles this for the manager's own SetCaching update, but not for updatx: keep a tick counter andonly run a QuerySource on a tick strictly after the one where its request
arrived, which guarantees applied first.
There was a problem hiding this comment.
The issue is real. I'll fix it.
There was a problem hiding this comment.
I would add that chance to encounter it in the real life is low.

Description
Old memory manager has been rewritten from the ground. Fixes media-cache deadlocks that can freeze scene switching and block source cleanup.
MediaCacheManagerworker that coalesces requests, manages cache reservations, and schedules bounded readiness retries.Motivation and Context
Support issue reports scene-switch freezes with animated overlays on Windows, Streamlabs Desktop 1.21.9.
A local reproduction using the supplied WebM files exposed a circular wait between media-cache workers holding different source locks. The graphics thread subsequently blocked during source activation, freezing video processing and preventing cleanup.
This change removes those competing worker locks and makes cache scheduling and source lifetime explicit. Product changes are confined to OSN and use the existing OBS media-source caching setting.
How Has This Been Tested?
Reproduced in a stand-alone test.
Tested via controlled time advances in unit tests + Manually. Windows only.
Types of changes